<template>
  <div class="position-group">
    <el-button type="primary" style="margin-bottom: 10px" @click="addRow(null)">
      添加根目录
    </el-button>
    <div v-if="isPage">
      <!--      一级循坏-->
      <transition-group appear
                        enter-active-class="animate__animated animate__fadeInDown"
                        leave-active-class="animate__animated animate__backOutDown"
      >
        <div v-for="(chapterMain,index) in chapterMainList"
             class="pronames-one"
             :class="{oneshow:chapterMain.isShow, onehide:!chapterMain.isShow}"
             :key="chapterMain.id">
          <div class="pronames-one-label">
            <div class="pronames-one-title" @click="onelist(chapterMain)">
              <el-icon>
                <Notebook/>
              </el-icon>
              <span class="pronames-one-index ml">第{{ index + 1 }}章</span>
              <span class="ml">{{ chapterMain.title }}</span>
            </div><div>
            <span class="mr" @click="editRow(chapterMain.id)">
              <el-icon><Edit/></el-icon>
            </span>
            <span class="mr" @click="addRow(chapterMain.id)">
              <el-icon><FolderAdd/></el-icon>
            </span>
            <el-popconfirm title="是否删除该章节" @confirm="deleteCourse(chapterMain.id)">
                <template #reference>
                  <span class="mr">
                    <el-icon><Delete/></el-icon>
                  </span>
                </template>
              </el-popconfirm>
            <span @click="onelist(chapterMain)">
                <el-icon>
                  <transition-group
                    enter-active-class="animate__animated animate__fadeInDown"
                    leave-active-class="animate__animated animate__backOutDown"
                  >
                    <ArrowDownBold v-if="chapterMain.isShow"/>
                    <ArrowUpBold v-else/>
                  </transition-group>
              </el-icon>
            </span>
            </div>
          </div>
          <!--          二级循环-->
          <transition-group
            enter-active-class="animate__animated animate__backInRight"
            leave-active-class="animate__animated animate__fadeOut"
          >
            <div
              v-for="(chapterVice,index) in chapterMain.chapterViceList"
              :key="chapterVice.id"
              class="pronames-two"
              v-show="chapterMain.isShow"
              :class="{twoshow:chapterVice.isShow,twohide:!chapterVice.isShow}"
            >
              <div>
                <div class="pronames-two-label">
                  <div class="pronames-two-title" @click="twolist(chapterMain,chapterVice)">
                    <el-icon>
                      <FolderOpened/>
                    </el-icon>
                    <span class="pronames-two-index ml">第{{ index + 1 }}节</span>
                    <span class="ml">{{ chapterVice.title }}</span>
                  </div>
                  <div>
                    <span class="mr" @click="editRow(chapterVice.id)">
                      <el-icon><Edit/></el-icon>
                    </span>
                    <span class="mr"
                          @click="uploadRow(chapterVice.id)">
                      <el-icon><FolderAdd/></el-icon>
                    </span>
                    <el-popconfirm title="是否删除该章节" @confirm="deleteCourse(chapterVice.id)">
                      <template #reference>
                        <span class="mr">
                          <el-icon><Delete/></el-icon>
                        </span>
                      </template>
                    </el-popconfirm>
                    <span @click="twolist(chapterMain,chapterVice)">
                        <el-icon>
                          <transition-group
                            enter-active-class="animate__animated animate__fadeInDown"
                            leave-active-class="animate__animated animate__backOutDown"
                          >
                          <ArrowDownBold v-if="chapterVice.isShow"/>
                          <ArrowUpBold v-else/>
                          </transition-group>
                        </el-icon>
                    </span>
                  </div>
                </div>
                <transition-group
                  enter-active-class="animate__animated animate__backInRight"
                  leave-active-class="animate__animated animate__zoomOut"
                >
                  <!--          三级循坏-->
                  <div class="pronames-three"
                       v-for="chapterViceSection in chapterVice.chapterViceSectionList"
                       :key="chapterViceSection.id"
                       v-show="chapterVice.isShow"
                  >
                    <div class="pronames-three-label">
                      {{ chapterViceSection.title }}
                      <div class="deleteSession">
                        <span @click="getCourseSession(chapterViceSection)">
                          <el-icon><HelpFilled /></el-icon>
                        </span>
                        <el-popconfirm title="是否删除该资源" @confirm="deleteCourseSession(chapterViceSection.sectionResourceId)">
                          <template #reference>
                          <span class="mr">
                            <el-icon><Delete/></el-icon>
                          </span>
                          </template>
                        </el-popconfirm>
                      </div>
                    </div>
                  </div>
                </transition-group>
              </div>
            </div>
          </transition-group>
        </div>
      </transition-group>
      <VideoPlay ref="videoRef" v-show="showVideo" />
    </div>
    <div style="width: 100%;" v-else>
      <el-empty description="该课程暂无章节，请添加章节信息"/>
    </div>
  </div>
</template>

<script lang='ts' setup>
import 'animate.css';
import {
  ref, onMounted, getCurrentInstance, defineExpose,
} from 'vue';
import { useRoute } from 'vue-router';
import Request from '@/api/request/request';
import { ElMessage } from 'element-plus';
import VideoPlay from '@/components/VideoPlay.vue';

const { proxy } = getCurrentInstance();
const route = useRoute();

const showVideo = ref<boolean>(false);
const videoRef = ref();
// 所有主章节集合
const chapterMainList = ref<any>();
// 课程现有的所有资源
const resourcesList = ref();
// 一级章节id
const chapterMainId = ref<number>();
// 主章节，用于传给父组件
const chapterMian = ref();
// 是否有章节
const isPage = ref<boolean>();
type chapter = {
  chapterMain?: any
}

// 获取该课程的所有主章节
const getCourseList = async () => {
  const courseId = sessionStorage.getItem('courseId');
  const resData: any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_SCHOOLCOURSESECTION_COURSEIDBYSECTION_MANAGE}?courseId=${courseId}`).get();
  if (resData.data.length > 0) {
    isPage.value = true;
    chapterMainList.value = resData.data;
  } else {
    isPage.value = false;
    chapterMainList.value = [];
  }
};

onMounted(() => {
  getCourseList();
});

// 获取该课程的指定主章节的子章节
const getChapterViceList = async (parentId: number) => {
  const courseId = sessionStorage.getItem('courseId');
  const resData: any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_SCHOOLCOURSESECTION_COURSEIDBYSECTION_MANAGE}?courseId=${courseId}&parentId=${parentId}`).get();
  if (resData.code === 200) {
    return resData.data;
  }
  return false;
};
// 获取子章节下的课程资源
// eslint-disable-next-line consistent-return
const getChapterViceSection = async (sectionId: string) => {
  let resData: any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_SCHOOLCOURSERESOURCE_GETRESOURCEBYSECTION_MANAGE}/${sectionId}`).get();
  if (resData.code === 200) {
    console.log(resData.data);
    return resData.data;
  }
};

// 删除章节
const deleteCourse = async (id: number) => {
  let result: any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_SCHOOLCOURSESECTION_MANAGE}?ids=${id}`).delete();
  if (result.success) {
    await getCourseList();
    ElMessage.success('删除成功');
  }
};
// 删除章节下的课程资源
const deleteCourseSession = async (id:number) => {
  let result: any = await new Request(`${proxy.$urls.API_URL_BACKSTAGE_SCHOOLCOURSESECTIONRESOURCE_MANAGE}?ids=${id}`).delete();
  if (result.success) {
    await getCourseList();
    ElMessage.success('删除成功');
  }
};

// 获取该课程下指定主章节的所有子章节
// 控制一级菜单展开收起
const onelist = async (chapterMain: any) => {
  // eslint-disable-next-line no-param-reassign
  chapterMain.chapterViceList = await getChapterViceList(chapterMain.id);
  // 判断一级列表下是否有二级列表，1：如果没有，弹出提示，并取消动画效果 2：如果有，则显示一级菜单下的二级，并进行动画效果
  if (chapterMain.chapterViceList.length <= 0) {
    ElMessage.error('该章节无子章节');
  } else {
    chapterMainList.value.forEach((item: any) => {
      if (item.isShow === undefined) {
        // eslint-disable-next-line no-param-reassign
        item.isShow = false;
        if (item.id === chapterMain.id) {
          // eslint-disable-next-line no-param-reassign
          chapterMain.isShow = true;
        }
      } else {
        if (item.id === chapterMain.id) {
          // eslint-disable-next-line no-param-reassign
          chapterMain.isShow = !chapterMain.isShow;
        }
      }
    });
  }
};
// 控制二级菜单展开收起
const twolist = async (chapterMain: any, chapterVice: any) => {
  // eslint-disable-next-line no-param-reassign
  chapterVice.chapterViceSectionList = await getChapterViceSection(chapterVice.id);
  // 判断二级列表下是否有三级列表，1：如果没有，弹出提示，并取消动画效果 2：如果有，则显示一级菜单下的二级，并进行动画效果
  if (chapterVice.chapterViceSectionList !== '') {
    chapterMain.chapterViceList.forEach((item: any) => {
      if (!item.isShow) {
        // eslint-disable-next-line no-param-reassign
        item.isShow = false;
        if (item.id === chapterVice.id) {
          // eslint-disable-next-line no-param-reassign
          chapterVice.isShow = true;
        }
      } else {
        if (item.id === chapterVice.id) {
          // eslint-disable-next-line no-param-reassign
          chapterVice.isShow = !chapterVice.isShow;
        }
      }
    });
  }
};

const getCourseSession = (val:any) => {
  const reg = RegExp(/(?:mp4|mvm)$/);
  if (reg.test(val.fileUrl)) {
    const url = `http://localhost:8071/${val.fileUrl}`;
    videoRef.value.openDia(url);
    showVideo.value = true;
  }
};

// 编辑章节信息弹窗
const editRow = (chapterMain: chapter) => {
  proxy.$emit('editRow', chapterMain);
};

// 控制课程章节添加的弹窗
const addRow = (parentId: number) => {
  // 接收一级章节id
  proxy.$emit('addRow', parentId);
};

// 上传文件
const uploadRow = (parentId: number) => {
  proxy.$emit('uploadRow', parentId);
};

defineExpose({
  getCourseList,
});

</script>

<style lang="scss" scoped>
.mr {
  margin-right: 10px;
}

.ml {
  margin-left: 10px;
}

.position-group {
  width: 100%;
  height: auto;
}

/*一级*/
.pronames-one {
  width: 100%;
  height: auto;
}

.pronames-one-label {
  width: 100%;
  height: 55px;
  line-height: 55px;
  /*background-color: #F5F5F5;*/
  box-sizing: border-box;
  padding: 0 1%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  border: 1px #C0C0C0 solid;
  border-radius: 8px;
  margin-bottom: 10px;
  cursor: pointer;
}

.pronames-one-title {
  font-size: 14px;
  font-family: 仿宋;
}

.pronames-one-index {
  font-size: 16px;
  font-weight: bold;
  color: #FF7F00;
}

.pronames-one-label:hover {
  box-shadow: 0 0 10px #ccc;
  transition: all .5s ease;
  transform: translate(1.1);
}

/*二级*/
.pronames-two {
  width: 100%;
  height: auto;
  /*transition: transform 0.8s ease;*/
}

.pronames-two-label {
  width: 98%;
  float: right;
  height: 55px;
  line-height: 55px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0px 1%;
  border: 1px #C0C0C0 solid;
  border-radius: 8px;
  margin-bottom: 10px;
  cursor: pointer;
}

.pronames-two-title {
  font-size: 14px;
  font-family: 仿宋;
}

.pronames-two-index {
  font-size: 16px;
  font-weight: bold;
  color: #B87333;
}

.pronames-two-label label {
  color: #aaa;
  font-size: 20px;
  /*transition: all 0.5s;*/
}

.pronames-two-label:hover {
  box-shadow: 0 0 10px #ccc;
  transition: all .5s ease;
  transform: translate(1.1);
}

/*三级*/
.pronames-three {
  width: 100%;
  height: auto;
}

.pronames-three-label {
  width: 93%;
  float: right;
  margin-left: 3%;
  height: 80px;
  line-height: 50px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  border: 1px #C0C0C0 solid;
  border-radius: 8px;
  align-items: center;
  margin-bottom: 10px;
  padding: 0px 1%;
  cursor: pointer;
}

.deleteSession {
  span {
    margin-right: 20px;
  }
}

.pronames-three-label:hover {
  box-shadow: 0 0 10px #ccc;
  transition: all .5s ease;
  transform: translate(1.1);
}

/* 展开收起效果 start */
.oneshow {
}

.oneshow .pronames-two {
  display: block;
}

.onehide {
}

.onehide .pronames-two {
  display: none;
}

/* 展开收起效果 end */
.twoshow {
}

.twoshow .pronames-three {
  display: block;
}

.twohide {
}

.twohide .pronames-three {
  display: none;
}

</style>
